﻿using System;
using System.Data.OleDb;

namespace TraditionalMalteseCuisine.App_Code.Operations
{
    public class WriteOp : ConnectionOp
    {
        //*************************************************
        //                   Methods
        //*************************************************
        #region OleDb Methods
        /// <summary>
        /// Executes a non query command.
        /// </summary>
        /// <param name="commandText">The SQL command text to execute.</param>
        /// <returns>True if execution was successful, otherwise False.</returns>
        public static bool ExecuteCommand(string commandText)
        {
            return ExecuteCommand(new OleDbCommand(commandText));
        }

        /// <summary>
        /// Executes a non query command.
        /// </summary>
        /// <param name="oledbCommand">The SQL command to execute.</param>
        /// <returns>True if execution was successful, otherwise False.</returns>
        public static bool ExecuteCommand(OleDbCommand oledbCommand)
        {
            bool successful = false;

            try
            {
                if (OpenOleDbConnection())
                {
                    oledbCommand.CommandTimeout = 0;
                    oledbCommand.Connection = OleDbConnection;
                    oledbCommand.ExecuteNonQuery();

                    successful = true;
                }
            }
            finally
            {
                CloseOleDbConnection();
            }

            return successful;
        }



        /// <summary>
        /// Executes a scalar command which returns an Int32 value.
        /// </summary>
        /// <param name="commandText">The OleDb command text to execute.</param>
        /// <returns>The result of the OleDb command text.</returns>
        public static int ExecuteWithIntResult(string commandText)
        {
            return ExecuteWithIntResult(new OleDbCommand(commandText));
        }

        /// <summary>
        /// Executes a scalar command which returns an Int32 value.
        /// </summary>
        /// <param name="oledbCommand">The OleDb command to execute.</param>
        /// <returns>The result of the OleDb command.</returns>
        public static int ExecuteWithIntResult(OleDbCommand oledbCommand)
        {
            object obj = ExecuteScalar(oledbCommand);

            return (((obj == null) || (obj == DBNull.Value)) ? -1 : Convert.ToInt32(obj));
        }



        /// <summary>
        /// Executes a scalar command which returns an String value.
        /// </summary>
        /// <param name="commandText">The OleDb command text to execute.</param>
        /// <returns>The result of the OleDb command text.</returns>
        public static string ExecuteWithStringResult(string commandText)
        {
            return ExecuteWithStringResult(new OleDbCommand(commandText));
        }

        /// <summary>
        /// Executes a scalar command which returns an String value.
        /// </summary>
        /// <param name="oledbCommand">The OleDb command to execute.</param>
        /// <returns>The result of the OleDb command.</returns>
        public static string ExecuteWithStringResult(OleDbCommand oledbCommand)
        {
            object obj = ExecuteScalar(oledbCommand);

            return (((obj == null) || (obj == DBNull.Value)) ? string.Empty : Convert.ToString(obj));
        }



        /// <summary>
        /// Executes a scalar command which returns an object.
        /// </summary>
        /// <param name="commandText">The OleDb command text to execute.</param>
        /// <returns>The result of the OleDb command text.</returns>
        public static object ExecuteScalar(string commandText)
        {
            return ExecuteScalar(new OleDbCommand(commandText));
        }

        /// <summary>
        /// Executes a scalar command which returns an object.
        /// </summary>
        /// <param name="oledbCommand">The OleDb command to execute.</param>
        /// <returns>The result of the OleDb command.</returns>
        public static object ExecuteScalar(OleDbCommand oledbCommand)
        {
            object obj = null;

            try
            {
                if (OpenOleDbConnection())
                {
                    oledbCommand.CommandTimeout = 0;
                    oledbCommand.Connection = OleDbConnection;
                    obj = oledbCommand.ExecuteScalar();
                }
            }
            finally
            {
                CloseOleDbConnection();
            }

            return obj;
        }
        #endregion
        //*************************************************
    }
}